/**
 * @param {string[]} words
 * @return {string}
 */
 var longestWord = function(words) {
  words.sort((a, b) => a.length - b.length);
  let map = new Map();
  let ret = "";
  words.map(e => {
      if(e.length === 1) {
          map.set(e, 1);
          if(ret.length === 0 || ret > e) ret = e; 
      } else {
          let c = map.get(e.slice(0, e.length - 1));
          if(c) {
              map.set(e, 1);
              if(ret.length < e.length || (ret.length === e.length && ret > e)) ret = e;
          }
      }
  });
  return ret;
};